package njust.edu.dao.impl;

import njust.edu.dao.DishTypeDao;
import njust.edu.dbutils.SimpleUtils;
import njust.edu.entity.Dish;
import njust.edu.entity.DishType;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DishTypeDaoImpl implements DishTypeDao {

    @Override
    public DishType getById(int id) {
        String sql = "select id,name from dish_type where id = ?";
        DishType dishType = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while(rs.next()){
                dishType  = new DishType(rs.getInt(1),rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return dishType;
    }

    @Override
    public void add(DishType dishType) {
        String sql = "insert into dish_type(name) values(?)";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, dishType.getName());
            ps.executeUpdate();
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
    }

    @Override
    public void delete(int id) {
        String sql = "delete from dish_type where id = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
    }

    @Override
    public List<DishType> getList() {
        String sql = "SELECT id,name FROM dish_type";
        List<DishType> DT = new ArrayList<>();
        DishType dishType = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                dishType  = new DishType(rs.getInt(1),rs.getString(2));
                DT.add(dishType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return DT;
    }

    @Override
    public BigDecimal getDishTypeSale(Integer id) {
        String sql="SELECT sum(sale) from dish where type_id = ?";
        BigDecimal count = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = SimpleUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while(rs.next()){
                count = rs.getBigDecimal(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        SimpleUtils.release(conn,ps,rs);
        return count;
    }

}

